package org.spolo.android.support.tween;

import org.spolo.android.support.nodetree.Vcanvas;

/**
 * 一个向量的差值器提供从图形当前中心点移动到目标点中间的插值,计算的插值是相差很小平均量例如:2.90,3.00,3.01..
 * @date 2012-5-22
 */
public class Vector2Interpolate extends InterpolateBase {
	private float[] end;
	private float howlong;
	private float lastdx = 0;
	private float lastdy = 0;

	/**
	 * @param v
	 *            所要操作的Vcanvas对象，
	 * 
	 * @param vector
	 *            所要移动X轴向的距离与Y轴向的距离
	 * @param time
	 *            持续的时间
	 */
	public Vector2Interpolate(Vcanvas v, float[] vector, long time, int type) {
		super(v, type);
		this.end = vector;
		this.howlong = time;
	}

	@Override
	public void stepIt(long tick) {
		float dx = this.end[0];
		float dy = this.end[1];

		float factor = ((tick - startTick) / this.howlong);
		if (factor > 1.0) {
			Target.setValues(this.changeType, dx - lastdx, dy - lastdy);
			this.isFinish = true;
			return;
		}
		float mx = factor * dx - lastdx;
		float my = factor * dy - lastdy;

		lastdx = factor * dx;
		lastdy = factor * dy;
		Target.setValues(this.changeType, mx, my);
		this.isFinish = factor > 1.0;
	}
}
